#include <stdio.h>
#include <string.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_smartconfig.h"
#include "esp_wpa2.h"
#include "driver/gpio.h"
#include "my_smartconfig.h"
#include "wifi.h"
#include "lwip/err.h"
#include "lwip/sys.h"

#define ESP_MAXIMUM_RETRY 1000
#define WIFI_LED_PIN GPIO_NUM_32
#define WIFI_LED_OPEN 0
#define WIFI_LED_CLOSE 1
#define DEFAULT_SCAN_LIST_SIZE 6
/*
   事件组允许每个事件有多个位、
   但我们只关心一个事件--我们是否用 IP 连接到了接入点？
   我们在最大重试次数后连接失败
*/
#define Wifi_Storage "Wifi_Storage"
#define wifikey_num 0
#define WIFIKEY_NUM GPIO_NUM_0

static const int CONNECTED_BIT = BIT0;     // 连接完成标志位
static const int ESPTOUCH_DONE_BIT = BIT1; // 智能配网完成标志位
static const int WIFI_FAIL_BIT = BIT1;
static int s_retry_num = 0; // 重新连接时间
static int wifi_is_init = 0;
int create_sm_task = 0;

static const char *TAG = "smartconfig_example";
static EventGroupHandle_t s_wifi_event_group;
// static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
static void smartconfig_example_task(void *parm);
static void wifi_init_sta(char *ssid, char *password);
// 联网指示灯状态
int wifi_led_state = 0;
static void Wifi_Led_Task(void *arg);
// 按键智能配网任务
static void Key_WifiConfig_Task(void *arg)
{
    printf("here\n");
    // 初始化按键引脚
    gpio_config_t key_gpio = {};
    key_gpio.pin_bit_mask = 1ULL << wifikey_num;
    key_gpio.mode = GPIO_MODE_INPUT;
    key_gpio.pull_up_en = 1;
    gpio_config(&key_gpio);
    nvs_handle_t my_handle;
    esp_err_t err = 0;

    static int count = 0;
    while (1)
    {
        if ((gpio_get_level(WIFIKEY_NUM) == 0) && count == 0) // 按下时第一次进入
        {
            printf("按键按下\n");
            vTaskDelay(5 / portTICK_PERIOD_MS); // 消抖
        }
        if (gpio_get_level(WIFIKEY_NUM) == 0) // 按下
        {
            count++;
            if (count > 100 || count == 100)
            {
                wifi_led_state = 1;
            }
        }
        if (gpio_get_level(WIFIKEY_NUM) == 1) // 松开
        {
            if (count > 100 || count == 100) // 长按时间达到五秒
            {
                count = 0;
                printf("开始自动配网\n");
                esp_err_t err = nvs_open(Wifi_Storage, NVS_READWRITE, &my_handle);
                err = nvs_erase_key(my_handle, "wifi_ssid");
                if (err != ESP_OK)
                {
                    ESP_LOGE(TAG, "Delete WiFi_SSID FAIL:%s!\n", esp_err_to_name(err));
                }
                err = nvs_erase_key(my_handle, "wifi_password");
                if (err != ESP_OK)
                {
                    ESP_LOGE(TAG, "Delete WiFi_PASSWORD FAIL:%s!\n", esp_err_to_name(err));
                }
                nvs_commit(my_handle);
                nvs_close(my_handle);
                printf("WIFI配置删除成功\n");
                esp_restart();
                // esp_wifi_restore();
            }
        }

        vTaskDelay(50 / portTICK_PERIOD_MS); // 延时50毫秒
    }
}

static void initialise_wifi(void)
{
    ESP_ERROR_CHECK(esp_netif_init());
    s_wifi_event_group = xEventGroupCreate();
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
    assert(sta_netif);

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &smartconfig_event_handler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &smartconfig_event_handler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &smartconfig_event_handler, NULL));

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_start());
}
void smartconfig_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
    // 打开Wifi_Storage
    nvs_handle_t my_handle;
    esp_err_t err = nvs_open(Wifi_Storage, NVS_READWRITE, &my_handle);
    /*从WIFI事件开始*/
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) /*如果检测到wifi站启动*/
    {
        wifi_led_state = 1;
        if (create_sm_task == 0)
        {
            xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 15, NULL);
        }
        // xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 15, NULL);
        esp_wifi_connect();
        // 设备进入 wifi 配网模式，闪两下停一下
        // xTaskCreate(Wifi_Led_Task, "Wifi_Led_Task", 1024, NULL, 3, NULL);
        // Wifi_Led(1);
    }
    else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) /*如果WIFI没连接上ap*/
    {
        wifi_led_state = 2;
        esp_wifi_connect();                                      /*再一次连接*/
        xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT); // 清楚连接完成标志位
        // 急闪
        // Wifi_Led(2);
    }
    else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
    {
        xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT); // 连接完成标志位置位
        // 状态灯三秒灭一秒亮
        wifi_led_state = 3;
        // Wifi_Led(3);
    }
    else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE)
    {
        ESP_LOGI(TAG, "Scan done");
        wifi_led_state = 1;
    }
    else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL)
    {
        ESP_LOGI(TAG, "Found channel");
        wifi_led_state = 2;
    }
    else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) /*当触发了 SC_EVENT_GOT_SSID_PSWD 事件时，event_data 就包含了关于 SSID 和密码的信息*/
    {
        wifi_led_state = 2;
        ESP_LOGI(TAG, "Got SSID and password");
        // wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
        // ESP_ERROR_CHECK(esp_wifi_init(&cfg));
        smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data;
        wifi_config_t wifi_config;
        uint8_t ssid[33] = {0};
        uint8_t password[65] = {0};
        uint8_t rvd_data[33] = {0};
        // 这边
        bzero(&wifi_config, sizeof(wifi_config_t));                                        /*清零 wifi_config_t 大小的内存块中的所有字节。*/
        memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));             /*获取ssid*/
        memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password)); /*获取password*/
        wifi_config.sta.bssid_set = evt->bssid_set;                                        // 是否设置目标MAC地址
        if (evt->bssid_set == true)
        {
            memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
        }

        uint16_t n_number = DEFAULT_SCAN_LIST_SIZE;         // 扫描最大个数
        wifi_ap_record_t n_ap_info[DEFAULT_SCAN_LIST_SIZE]; // 扫描wifi存储数组

        uint16_t n_ap_count = 0;                 // ap数量
        int8_t n_rssi = -100;                    // 存储信号强度
        memset(n_ap_info, 0, sizeof(n_ap_info)); // 数组清空
        // ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
        // ESP_ERROR_CHECK(esp_wifi_start());

        wifi_scan_config_t n_wifi_scan = {
            .ssid = wifi_config.sta.ssid,
        };
        esp_wifi_scan_start(&n_wifi_scan, true);                             // 扫描所有可用ap
        ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&n_number, n_ap_info)); // 获取上次扫描中发现的AP列表
        ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&n_ap_count));
        // 获取上次扫描中发现的ap数量
        ESP_LOGI(TAG, "Total APs scanned = %u", n_ap_count);
        // 打印符合ssid的ap数量
        for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < n_ap_count); i++)
        {
            ESP_LOGI(TAG, "RSSI \t\t%d", n_ap_info[i].rssi);
            if (n_ap_info[i].rssi > n_rssi) // 如果信号高于上一个
            {
                n_rssi = n_ap_info[i].rssi; // 更新信号值
                wifi_config.sta.bssid[0] = n_ap_info[i].bssid[0];
                wifi_config.sta.bssid[1] = n_ap_info[i].bssid[1];
                wifi_config.sta.bssid[2] = n_ap_info[i].bssid[2];
                wifi_config.sta.bssid[3] = n_ap_info[i].bssid[3];
                wifi_config.sta.bssid[4] = n_ap_info[i].bssid[4];
                wifi_config.sta.bssid[5] = n_ap_info[i].bssid[5];
                wifi_config.sta.bssid_set = 1;
            }
        }
        ESP_LOGI(TAG, "最高RSSI \t\t%d", n_rssi);
        esp_wifi_stop();

        memcpy(ssid, evt->ssid, sizeof(evt->ssid));             /*获取ssid*/
        memcpy(password, evt->password, sizeof(evt->password)); /*获取password*/
        // 存储ssid以及password
        err = nvs_set_str(my_handle, "wifi_ssid", &ssid);
        if (err != ESP_OK)
        {
            ESP_LOGE(TAG, "wifi_ssid save error!!!");
        }
        nvs_set_str(my_handle, "wifi_password", &password);
        // 提交
        err = nvs_commit(my_handle);
        if (err != ESP_OK)
        {
            ESP_LOGE(TAG, "nvs_commit error!!!");
            return;
        }
        ESP_LOGI(TAG, "SSID:%s", ssid);
        ESP_LOGI(TAG, "PASSWORD:%s", password);
        if (evt->type == SC_TYPE_ESPTOUCH_V2)
        {
            ESP_ERROR_CHECK(esp_smartconfig_get_rvd_data(rvd_data, sizeof(rvd_data)));
            ESP_LOGI(TAG, "RVD_DATA:");
            for (int i = 0; i < 33; i++)
            {
                printf("%02x ", rvd_data[i]);
            }
            printf("\n");
        }
        // esp_err_t err;
        //  ESP_ERROR_CHECK(esp_wifi_disconnect()); // 断开连接
        //  err = esp_wifi_disconnect();
        //  if (err != ESP_OK)
        //  {
        //      ESP_LOGE(TAG, "esp_wifi_disconnect:%s!\n", esp_err_to_name(err));
        //  }
        wifi_led_state = 0;
        ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
        ESP_ERROR_CHECK(esp_wifi_start());
        esp_wifi_connect();
    }
    else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) /*智能网配向手机发送应答*/
    {
        xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT); /*智能网配完成标志位置位*/
        wifi_led_state = 3;
    }
    nvs_close(my_handle);
}

static void smartconfig_example_task(void *parm)
{
    create_sm_task = 1; // 创建标志
    EventBits_t uxBits;
    // 一、设置smartconfig协议方式
    ESP_ERROR_CHECK(esp_smartconfig_set_type(SC_TYPE_ESPTOUCH));
    // 配置默认smartconfig
    smartconfig_start_config_t erg = SMARTCONFIG_START_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_smartconfig_start(&erg));
    while (1)
    {
        uxBits = xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
        if (uxBits & CONNECTED_BIT) // 连接完成
        {
            ESP_LOGI(TAG, "WiFi Connected to ap");
        }
        if (uxBits & ESPTOUCH_DONE_BIT)
        {
            ESP_LOGI(TAG, "smartconfig over");
            esp_smartconfig_stop(); /*停止智能配置，释放 esp_smartconfig_start 占用的缓冲区。*/
            vTaskDelete(NULL);      /*删除当前任务*/
        }
        vTaskDelay(100 / portTICK_PERIOD_MS);
    }
}

void connect_to_wifi(void)
{
    nvs_handle_t my_handle;
    char wifi_ssid[33] = {0};
    // uint8_t wifisiid_len = 0;
    char wifi_password[65] = {0};
    // uint8_t wifipassword_len = 0;
    size_t required_size;
    wifi_is_init = 0;

    ESP_ERROR_CHECK(nvs_flash_init());
    esp_err_t err = nvs_open(Wifi_Storage, NVS_READWRITE, &my_handle);
    size_t count = 0;
    err = nvs_get_str(my_handle, "wifi_ssid", NULL, &count);
    err = nvs_get_str(my_handle, "wifi_ssid", wifi_ssid, &count);
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "READ WiFi_SSID FAIL:%s!\n", esp_err_to_name(err));
    }
    else
    {
        ESP_LOGI(TAG, "WiFi_SSID:%s\n", wifi_ssid);
        wifi_is_init++;
    }
    count = 0;
    err = nvs_get_str(my_handle, "wifi_password", NULL, &count);
    err = nvs_get_str(my_handle, "wifi_password", wifi_password, &count);
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "READ WiFi_PASSWORD FAIL:%s!\n", esp_err_to_name(err));
    }
    else
    {
        ESP_LOGI(TAG, "WiFi_PASSWORD:%s\n", wifi_password);
        wifi_is_init++;
    }
    nvs_close(my_handle);
    // 执行wifi状态指示灯任务
    // 执行按键智能配网任务
    xTaskCreate(Key_WifiConfig_Task, "Key_WifiConfig_Task", 4096, NULL, 16, NULL);
    xTaskCreate(Wifi_Led_Task, "Wifi_Led_Task", 4096, NULL, 20, NULL);
    if (wifi_is_init == 2)
    {
        // 不执行配网
        ESP_LOGI(TAG, "start connect wifi");
        wifi_led_state = 2;
        wifi_init_sta((char *)wifi_ssid, (char *)wifi_password);
    }
    else
    {
        // 执行配网
        ESP_LOGI(TAG, "start smartconfig");
        wifi_led_state = 1;
        initialise_wifi();
    }
}

// 事件处理函数
void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
    {
        wifi_led_state = 2;
        esp_wifi_connect();
    }
    else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
    {
        if (s_retry_num < ESP_MAXIMUM_RETRY)
        {
            wifi_led_state = 2;
            esp_wifi_connect();
            s_retry_num++;
            ESP_LOGI(TAG, "retry to connect to the AP");
        }
        else
        {
            wifi_led_state = 0;
            xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
        }
        ESP_LOGI(TAG, "connect to the AP fail");
    }
    else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
    {
        ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
        ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
        s_retry_num = 0; // 重连时间清零
        wifi_led_state = 3;
        xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
    }
}

static void wifi_init_sta(char *ssid, char *password)
{
    s_wifi_event_group = xEventGroupCreate();

    ESP_ERROR_CHECK(esp_netif_init());

    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_create_default_wifi_sta();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    uint16_t n_number = DEFAULT_SCAN_LIST_SIZE;         // 扫描最大个数
    wifi_ap_record_t n_ap_info[DEFAULT_SCAN_LIST_SIZE]; // 扫描wifi存储数组
    uint16_t n_ap_count = 0;                            // ap数量
    int8_t n_rssi = -100;
    memset(n_ap_info, 0, sizeof(n_ap_info)); // 数组清空

    wifi_config_t wifi_config = {
        .sta = {
            /* Setting a password implies station will connect to all security modes including WEP/WPA.
             * However these modes are deprecated and not advisable to be used. Incase your Access point
             * doesn't support WPA2, these mode can be enabled by commenting below line */
            .threshold.authmode = WIFI_AUTH_WPA2_PSK,
            .sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
            // .pmf_cfg = {
            //     .capable = true,
            //     .required = false},
        },
    };
    strcpy((char *)wifi_config.sta.ssid, ssid);
    strcpy((char *)wifi_config.sta.password, password);
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_start());
    wifi_scan_config_t n_wifi_scan = {
        .ssid = wifi_config.sta.ssid,
    };
    esp_wifi_scan_start(&n_wifi_scan, true);                             // 扫描所有可用ap
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&n_number, n_ap_info)); // 获取上次扫描中发现的AP列表
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&n_ap_count));
    // 获取上次扫描中发现的ap数量
    ESP_LOGI(TAG, "Total APs scanned = %u", n_ap_count);
    // 打印符合ssid的ap数量
    for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < n_ap_count); i++)
    {
        ESP_LOGI(TAG, "RSSI \t\t%d", n_ap_info[i].rssi);
        if (n_ap_info[i].rssi > n_rssi) // 如果信号高于上一个
        {
            n_rssi = n_ap_info[i].rssi; // 更新信号值
            wifi_config.sta.bssid[0] = n_ap_info[i].bssid[0];
            wifi_config.sta.bssid[1] = n_ap_info[i].bssid[1];
            wifi_config.sta.bssid[2] = n_ap_info[i].bssid[2];
            wifi_config.sta.bssid[3] = n_ap_info[i].bssid[3];
            wifi_config.sta.bssid[4] = n_ap_info[i].bssid[4];
            wifi_config.sta.bssid[5] = n_ap_info[i].bssid[5];
            wifi_config.sta.bssid_set = 1;
        }
    }
    ESP_LOGI(TAG, "最高RSSI \t\t%d", n_rssi);
    esp_wifi_stop();

    ESP_LOGI(TAG, "wifi_init_sta finished.");

    esp_event_handler_instance_t instance_any_id;
    esp_event_handler_instance_t instance_got_ip;
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &wifi_event_handler,
                                                        NULL,
                                                        &instance_any_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &wifi_event_handler,
                                                        NULL,
                                                        &instance_got_ip));
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());
    /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
     * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
    EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
                                           CONNECTED_BIT | WIFI_FAIL_BIT,
                                           pdFALSE,
                                           pdFALSE,
                                           portMAX_DELAY);

    /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
     * happened. */
    if (bits & CONNECTED_BIT)
    {
        ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", ssid, password);
    }
    else if (bits & WIFI_FAIL_BIT)
    {
        ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", ssid, password);
    }
    else
    {
        ESP_LOGE(TAG, "UNEXPECTED EVENT");
    }

    /* The event will not be processed after unregister */
    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
    ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
    vEventGroupDelete(s_wifi_event_group);
}

// 联网指示灯状态
static void Wifi_Led_Task(void *arg)
{
    while (1)
    {
        if (wifi_led_state == 1) // 设备进入 wifi 配网模式
        {
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_OPEN);
            vTaskDelay(100 / portTICK_PERIOD_MS); // 延时100ms
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_CLOSE);
            vTaskDelay(100 / portTICK_PERIOD_MS); // 延时100ms
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_OPEN);
            vTaskDelay(100 / portTICK_PERIOD_MS); // 延时100ms
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_CLOSE);
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_CLOSE);
            vTaskDelay(700 / portTICK_PERIOD_MS); // 延时700ms
        }
        if (wifi_led_state == 2) // 设备收到 wifi 信息，正在连接wifi
        {
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_OPEN);
            vTaskDelay(100 / portTICK_PERIOD_MS); // 延时100ms
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_CLOSE);
            vTaskDelay(100 / portTICK_PERIOD_MS); // 延时100ms
        }
        if (wifi_led_state == 3) // 设备已连接 wifi
        {
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_CLOSE);
            vTaskDelay(3000 / portTICK_PERIOD_MS); // 延时3s
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_OPEN);
            vTaskDelay(1000 / portTICK_PERIOD_MS); // 延时1s
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_CLOSE);
        }
        if (wifi_led_state == 0)
        {
            gpio_set_level(WIFI_LED_PIN, WIFI_LED_CLOSE);
            vTaskDelay(100 / portTICK_PERIOD_MS); // 延时1ms
        }

        vTaskDelay(100 / portTICK_PERIOD_MS); // 延时1ms
    }
}